﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using StockManagement.ViewModels;
using System.Collections.Specialized;

namespace StockManagement.Views
{
    /// <summary>
    /// Interaction logic for ProductsPage.xaml 
    /// </summary>
    public partial class ProductsPage : Frame
    {
        NewProduct _newProductFrame;
        EditProduct _editProductFrame;

        ProductPageViewModel _productPageVM;

        public ProductsPage()
        {
            InitializeComponent();
            DataContextChanged += new DependencyPropertyChangedEventHandler(ProductsPage_DataContextChanged);
        }

        void ProductsPage_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue == null)
                return;
            var prodPageVM = e.NewValue as ProductPageViewModel;
            if (prodPageVM == null)
                return;
            SetMainGridDataContext(prodPageVM);
        }

        private void SetMainGridDataContext(ProductPageViewModel productPageVM)
        {
            _productPageVM = productPageVM;
            _productPageVM.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(_productPageVM_PropertyChanged);
            _mainGrid.DataContext = _productPageVM;
            if (addeditFrame.Content == null)
                rdAddProduct.IsChecked = true;
        }

        void _productPageVM_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            SetDataContextForAddEditFrame();
        }

        private void rdAddProduct_Checked(object sender, RoutedEventArgs e)
        {
            if (_productPageVM == null)
                return;

            switch (rdAddProduct.IsChecked)
            {
                case true:
                    if (_newProductFrame == null)
                        _newProductFrame = new NewProduct();

                    addeditFrame.Content = _newProductFrame;
                    break;
                case false:
                    if (_editProductFrame == null)
                        _editProductFrame = new EditProduct();

                    addeditFrame.Content = _editProductFrame;
                    break;
                default:
                    break;
            }

            SetDataContextForAddEditFrame();
        }

        private void SetDataContextForAddEditFrame()
        {
            switch (rdAddProduct.IsChecked)
            {
                case true:
                    _newProductFrame.DataContext = _productPageVM.NewProduct;
                    break;
                case false:
                    _editProductFrame.DataContext = _productPageVM.EditProduct;
                    break;
                default:
                    break;
            }            
        }
    }
}
